package com.OnTimeTeam.OnTime;


import java.util.ArrayList;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.ImageButton;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapActivity extends Activity  {

	
	private LatLng ORIGIN;
	private Marker originMarker;
	
	private LatLng DEST;
	private Marker destMarker;
	
	private GoogleMap map;
	private ImageButton satButton;
	private ImageButton trafficButton;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.show_map);
		
		
		satButton = (ImageButton)findViewById(R.id.satButton);
		trafficButton = (ImageButton)findViewById(R.id.trafficButton);
		
		
	    ORIGIN = new LatLng(getIntent().getExtras().getDouble("srcLat"), getIntent().getExtras().getDouble("srcLng"));
	    DEST = new LatLng(getIntent().getExtras().getDouble("dstLat"), getIntent().getExtras().getDouble("dstLng"));
        
	    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
	    map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(ORIGIN, 15));
        map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
	    
        originMarker = map.addMarker(new MarkerOptions().position(ORIGIN).title("Origin")
                .snippet(getIntent().getExtras().getString("origin")).icon(BitmapDescriptorFactory
    	                .fromResource(R.drawable.point_a))); 
        destMarker = map.addMarker(new MarkerOptions().position(DEST).title("Destination")
                .snippet(getIntent().getExtras().getString("dest")).icon(BitmapDescriptorFactory
    	                .fromResource(R.drawable.point_b)));    
        
		satButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(map.getMapType() == GoogleMap.MAP_TYPE_SATELLITE)
					map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
				else
					map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
			}
		});
		
		trafficButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(!map.isTrafficEnabled())
					map.setTrafficEnabled(true);
				else
					map.setTrafficEnabled(false);
			}
		});

        
    	try {
    		

			
			ArrayList<LatLng> alRoute = new ArrayList<LatLng>();
			double[] coordsArray = getIntent().getExtras().getDoubleArray("coords");
			
			double lat = 0, lng = 0;
			for(int i = 0; i < coordsArray.length; ++i){
				if(i%2 == 0)
					lat = coordsArray[i];
				else {
					lng = coordsArray[i];
					alRoute.add(new LatLng(lat, lng));
				}	
			}
			
			
            PolylineOptions routeDrawing = new PolylineOptions();
            routeDrawing.color(Color.parseColor("#33B5E5"));
            
            Log.e("SIZE", Integer.toString(alRoute.size()));
            
            for(int i = 0; i < alRoute.size(); ++i)
            	routeDrawing.add(alRoute.get(i));
            
            // Get back the mutable Polyline
            Polyline polyline = map.addPolyline(routeDrawing);
            
		} catch (Exception e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
